<script setup lang="ts">
import type { ActionSheetAction } from 'vant';
import { onMounted, ref } from 'vue';

const actions = [
  { name: '相册' },
  { name: '拍照' }
]

// 1. 修改头像
// 1.1 控制修改头像弹层的显示/隐藏
const showAvatarSheet = ref(false)
// 1.2 打开相册/相机
const onSelectAvatar = (item: ActionSheetAction, index: number) => {
  console.log('选择相册/相机时触发-onSelectAvatar', item, index);

}
// 是否显示生日弹层
const showBirthdayPopup = ref(false)
const onChangeBirthday = (date: Date) => {
  console.log('选择生日时触发-onChangeBirthday', date);
  showBirthdayPopup.value = false
}
const areaColumns = ref<AreaColumns>()

onMounted(async () => {
  const data = await mk.getAreaColumns()
  areaColumns.value = data
  console.log(data)
})
// 4. 选择职业
const columns = [
  { text: '软件工程师', value: '软件工程师' },
  { text: '医生', value: '医生' },
  { text: '教师', value: '教师' },
  { text: '律师', value: '律师' },
  { text: '会计师', value: '会计师' },
  { text: '销售经理', value: '销售经理' },
  { text: '市场营销专员', value: '市场营销专员' },
  { text: '建筑师', value: '建筑师' },
  { text: '护士', value: '护士' },
  { text: '机械工程师', value: '机械工程师' }
]
// 是否显示职业弹层
const showProfessionPopup = ref(false)
const onChangeProfession = (data: { selectedValues: number[] }) => {
  showProfessionPopup.value = false
  console.log(data);
}

// 省市区数据
const areaList = ref<AreaColumns>({
  province_list: {
    110000: '北京',
    120000: '天津',
  },
  city_list: {
    110100: '北京市',
    120100: '天津市',
  },
  county_list: {
    110101: '东城区',
    110102: '西城区',
    120101: '天津1区',
    120102: '天津2区',
  },
})
const showAreaPopup = ref(false)
onMounted(async () => {
  const res = await mk.getAreaColumns()
  // console.log(res)
  areaList.value = res
})

</script>

<template>
  <div class="profile-edit-page">
    <!-- 头像部分 -->
    <div class="avatar">
      <van-image @click="showAvatarSheet = true" round width="100" height="100" class="avatar-img"
        src="https://foruda.gitee.com/avatar/1716533299289369832/5539886_yjh8866_1716533299.png">
      </van-image>
      <div class="avatar-btn">
        <span @click="showAvatarSheet = true">修改头像</span>
      </div>
    </div>
    <!-- 动作面板组件，类似鸿蒙半模态弹窗 -->
    <van-action-sheet v-model:show="showAvatarSheet" :actions="actions" @select="onSelectAvatar" cancel-text="取消"
      close-on-click-action />

    <!-- 中间表单部分 -->
    <van-cell-group>
      <van-field label="账号" readonly></van-field>
      <van-field label="昵称" placeholder="请输入昵称"></van-field>
      <van-cell title="性别" class="gender">
        <van-radio-group :icon-size="16">
          <van-radio name="男">男</van-radio>
          <van-radio name="女">女</van-radio>
          <van-radio name="未知">未知</van-radio>
        </van-radio-group>
      </van-cell>
      <van-field @click="showBirthdayPopup = true" label="生日" readonly placeholder="请选择日期"></van-field>
      <van-field @click="showAreaPopup = true" label="所在地" readonly placeholder="请选择所在地"></van-field>
      <van-field @click="showProfessionPopup = true" label="职业" readonly placeholder="请选择职业"></van-field>
    </van-cell-group>

    <div class="submit">
      <van-button round block type="primary">
        保存资料
      </van-button>
    </div>
    <!-- 选择生日弹层  位置是底部 -->
    <van-popup v-model:show="showBirthdayPopup" round position="bottom">
      <!-- 日期选择器 -->
      <van-date-picker title="选择日期" @cancel="showBirthdayPopup = false" @confirm="onChangeBirthday"
        :min-date="new Date('1980-01-01')" :max-date="new Date" />
    </van-popup>
    <van-field label="职业" readonly placeholder="请选择职业" @click="showProfessionPopup = true"></van-field>
    <!-- 打开选择职业弹层 -->
    <van-popup v-model:show="showProfessionPopup" round position="bottom">
      <van-picker :columns="columns" @confirm="onChangeProfession" @cancel="showProfessionPopup = false" />
    </van-popup>
    
    <van-popup v-model:show="showAreaPopup" round position="bottom">
      <!-- 省市区选择器 -->
      <van-area title="请选择省市区" :area-list="areaList" />
    </van-popup>


  </div>
</template>

<style scoped>
.avatar {
  padding: 30px;
  text-align: center;
  background-color: var(--mk-white);
}

.avatar-img {
  box-shadow: 0 0 5px var(--mk-gray);
}

.avatar-btn {
  color: var(--mk-linear_end);
  margin-top: 10px;
}

.gender ::v-deep(.van-cell__title) {
  width: 100px;
  flex: none;
}

.van-radio-group {
  display: flex;
  justify-content: space-between;
  height: 36px;
  width: 180px;
}

.submit {
  padding: 16px;
}
</style>